package cn.element.io.util;

import java.io.*;

/**
 * 实现文件拷贝工具类
 */
public class FileUtils {

    /**
     * 文件拷贝方法
     * @param pos       源位置
     * @param des       目的位置
     */
    public static void copyFile(String pos, String des) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(pos));
            bos = new BufferedOutputStream(new FileOutputStream(des));

            int temp;

            while ((temp = bis.read()) != -1) {  //BufferedInputStream类内部已经有缓冲数组了,所以不需要外部定义
                bos.write(temp);
            }

            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 使用字符流来实现文件拷贝
     * @param pos       源位置
     * @param des       目的位置
     */
    public static void copyFileWithWriter(String pos, String des) {
        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(pos);
            fw = new FileWriter(des);

            char[] buffer = new char[1024];  //创建数组缓冲区

            int temp;

            while ((temp = fr.read(buffer)) != -1) {
                fw.write(buffer, 0, temp);
            }

            fw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 基于字符缓冲流拷贝文件
     */
    public static void copyFileWithBufferedWriter(String pos, String des) {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            br = new BufferedReader(new FileReader(pos));
            bw = new BufferedWriter(new FileWriter(des));

            String s = "";

            while ((s = br.readLine()) != null) {  //读一行换一行
                bw.write(s);
                bw.newLine();
            }

            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }





}
